package com.yun.algorithmproblem.leetcode;

public class Leetcode1706 {
    public static void main(String[] args) {
        Leetcode1706 obj=new Leetcode1706();
        int[][] grid=new int[][]{{1,1,1,-1,-1},{1,1,1,-1,-1},{-1,-1,-1,1,1},{1,1,1,1,-1},{-1,-1,-1,-1,-1}};
        obj.findBall(grid);
    }


    public int[] findBall(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[] ans=new int[n];
        for (int i = 0; i < n; i++) {
            int p=0,q=i;
            while (p<m){
                if(grid[p][q]==1){//向右
                    if(q+1>n-1 || grid[p][q+1]==-1){
                        break;
                    }
                    q++;
                }else {//向左
                    if(q-1<0|| grid[p][q-1]==1){
                        break;
                    }
                    q--;
                }
                p++;
            }
            if(p==m){
                ans[i]=q;
            }else {
                ans[i]=-1;
            }
        }
        return ans;
    }
}
